/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;

/**
 *
 * @author Administrator
 */
public class MainGame extends GameCanvas implements Runnable, PlayerListener {

    //midlet
    private MMIDlet midlet;
    //private boolean isKenaDeh = true;
    //------------------------------------------//
    //SPRITE
    private int JUM = 40;
    private int MAX_RANGE = 12;
    private MatterBlock matterBlock[] = new MatterBlock[JUM];
    private Sprite spAncurBlock[] = new Sprite[JUM];
    private Sprite spMoroEnemy;
    private Sprite spMoroMine;
    private Sprite spAlarmMo;
    private int xAlarmMo;
    private int yAlarmMo;
    private int xMoroEnemy;
    private int yMoroEnemy;
    private int xMoroMine;
    private int yMoroMine;
    private Sprite spSet;
    private int xMatterBlock[] = new int[JUM];
    private int yMatterBlock[] = new int[JUM];
    private static int FRAME_ANCURBLOCK_WIDTH = 74;
    private static int FRAME_ANCURBLOCK_HEIGHT = 36;
    private boolean running = false;
    private LayerManager lmgr;
    //------------------------------------------//
    //GRID
    private int[][] grid = new int[4][10]; //40 x 10;
    //------------------------------------------//
    //speed
    private int speedGame = 0; //200 - 100, 0
    private int WIDTH = 320;
    private int HEIGHT = 240;
    private int speedMatterBlock = 1;
    //------------------------------------------//
    //image
    private Image imgMoro;
    private Image imgAncurBlockRed;
    private Image imgAncurBlockYellow;
    private Image imgAncurBlockBlue;
    private Image imgSetBarRed;
    private Image imgSetBarYellow;
    private Image imgSetBarBlue;
    private Image imgBarBowl;
    private Image imgBgBowl;
    private Image imgSetResultBar;
    private Image imgSet;
    private Image imgPortalLimit;
    private int iimgSetBarWidth = 0;
    private int iimgSetBarHeight = 0;
    private int iimgBarBowlWidth = 0;
    private int iimgBarBowlHeight = 0;
    private int iimgBgBowlWidth = 0;
    private int iimgBgBowlHeight = 0;
    private int iimgSetResultBarWidth = 0;
    private int iimgSetResultBarHeight = 0;
    //------------------------------------------//
    //jarak
    private int iJarak = 10;
    //------------------------------------------//
    //xy
    //private int xMenu = 0;    
    private int xBarBowl = 0;
    private int yBarBowl = 0;
    private int xBgBowl = 0;
    private int yBgBowl = 0;
    private int xSetResultBar = 0;
    private int ySetResultBar = 0;
    private int dasar;
    //for Random
    private int lastIDforRandom = 0;
    private boolean ulangi = false;
    //------------------------------------------//
    //boolean
    //private boolean isStack = false;
    private boolean isSama = false;
    //------------------------------------------//
    //STRING
    private String sAnswer = "";
    private int xAnswer = 0;
    private int yAnswer = 0;
    //private Question sQuestion[] = new Question[JUM];
    //------------------------------------------//
    //RANDOM
    private Random myRandom = new Random();
    //private int posisi[] = new int[JUM];
    //------------------------------------------//
    //for sound
    private long delay;
    private Player sound01;
    //------------------------------------------//

    public MainGame(MMIDlet midlet) {
        super(false);
        setFullScreenMode(true);

        this.midlet = midlet;

        FRAME_ANCURBLOCK_WIDTH = 74;
        FRAME_ANCURBLOCK_HEIGHT = 36;

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 10; j++) {
                //grid[i][j] = 10;
            }
        }

        try {
            imgPortalLimit = Image.createImage("/set_portallimit_190x6.png");
            imgMoro = Image.createImage("/moro_45x50.png");
            imgAncurBlockRed = Image.createImage("/set_barredclink_60x28.png");
            imgAncurBlockYellow = Image.createImage("/set_baryellowclink_60x28.png");
            imgAncurBlockBlue = Image.createImage("/set_barblueclink_60x28.png");
            imgSetBarRed = Image.createImage("/set_barred_58x19.png");
            imgSetBarYellow = Image.createImage("/set_baryellow_58x19.png");
            imgSetBarBlue = Image.createImage("/set_barblue_58x19.png");
            imgSetResultBar = Image.createImage("/set_barscore_240x29.png");
            //imgBgBowl = Image.createImage("/set_bgbowl_234x208.png");
            imgBgBowl = Image.createImage("/set_bgbowl_240x211.png");
            imgBarBowl = Image.createImage("/set_barbowl_238x210.png");

            if (MMIDlet.LANDSCAPE == 1) {
                imgSet = Image.createImage("/set_landscape_80x240.png");
            } else {
                imgSet = Image.createImage("/set_portrait_80x240.png");
            }

            iimgSetBarWidth = imgSetBarRed.getWidth();
            iimgSetBarHeight = imgSetBarRed.getWidth();
            iimgBgBowlWidth = imgBgBowl.getWidth();
            iimgBgBowlHeight = imgBgBowl.getHeight();
            iimgBarBowlWidth = imgBarBowl.getWidth();
            iimgBarBowlHeight = imgBarBowl.getHeight();
            iimgSetResultBarWidth = imgSetResultBar.getWidth();
            iimgSetResultBarHeight = imgSetResultBar.getHeight();

            spSet = new Sprite(imgSet);
            spMoroEnemy = new Sprite(Image.createImage(imgMoro), 45, 50);
            spMoroMine = new Sprite(Image.createImage(imgMoro), 45, 50);
            spAlarmMo = new Sprite(Image.createImage("/alarmo_12x17.png"), 12, 17);

            for (int i = 0; i < JUM; i++) {
                int warna = getRandomInt(3);
                if (warna == 0) {
                    matterBlock[i] = new MatterBlock(imgSetBarRed,
                            xMatterBlock[i], yMatterBlock[i]);
                    spAncurBlock[i] = new Sprite(imgAncurBlockRed,
                            60, 28);
                } else if (warna == 1) {
                    matterBlock[i] = new MatterBlock(imgSetBarYellow,
                            xMatterBlock[i], yMatterBlock[i]);
                    spAncurBlock[i] = new Sprite(imgAncurBlockYellow,
                            60, 28);
                } else if (warna == 2) {
                    matterBlock[i] = new MatterBlock(imgSetBarBlue,
                            xMatterBlock[i], yMatterBlock[i]);
                    spAncurBlock[i] = new Sprite(imgAncurBlockBlue,
                            60, 28);
                }
            }
            randomQuestion();

            // create and add to layer manager
            lmgr = new LayerManager();
            //lmgr.append(spPuato);
            for (int i = 0; i < JUM; i++) {
                lmgr.append(matterBlock[i]);
                lmgr.append(spAncurBlock[i]);
                lmgr.append(spMoroEnemy);
                lmgr.append(spMoroMine);
                spMoroEnemy.setVisible(false);
                spMoroMine.setVisible(false);
                lmgr.append(spAlarmMo);
                lmgr.append(spSet);


                //asalna matterBlock[i].setVisible(false);
                //asalna spAncurBlock[i].setVisible(false);
            }
            //spAncurBlock[0].setVisible(true);
        } catch (Exception e) {
        }

        if (MMIDlet.LANDSCAPE == 1) {

            xBarBowl = MMIDlet.WIDTH - iimgBarBowlWidth;
            yBarBowl = 0;
            xBgBowl = MMIDlet.WIDTH - iimgBarBowlWidth - 2;
            yBgBowl = 0;
            xSetResultBar = MMIDlet.WIDTH - iimgSetResultBarWidth;
            ySetResultBar = MMIDlet.HEIGHT - iimgSetResultBarHeight;

            for (int i = 0; i < JUM; i++) {
                xMatterBlock[i] = 1 + xBgBowl + (iimgSetBarWidth * getRandomInt(4));
                yMatterBlock[i] = yBgBowl - (i * 4 * iJarak);
            }

            dasar = imgBgBowl.getHeight() - imgSetBarRed.getHeight();
            xAnswer = xSetResultBar + 170;
            yAnswer = ySetResultBar + 5;

            xAlarmMo = 50;
            yAlarmMo = 25;
            xMoroEnemy = 20;
            yMoroEnemy = 80;
            xMoroMine = 200;
            yMoroMine = 50;
        } else {

            xBarBowl = MMIDlet.WIDTH - iimgBarBowlWidth;
            yBarBowl = MMIDlet.HEIGHT - iimgBarBowlHeight -
                    iimgSetBarHeight;
            xBgBowl = MMIDlet.WIDTH - iimgBgBowlWidth - 2;
            yBgBowl = MMIDlet.HEIGHT - (iimgBgBowlHeight +
                    iimgSetBarHeight + 10);
            xSetResultBar = MMIDlet.WIDTH - iimgSetResultBarWidth;
            ySetResultBar = MMIDlet.HEIGHT - iimgSetResultBarHeight;

            //random x,y position for matterBlock
            for (int i = 0; i < JUM; i++) {
                xMatterBlock[i] = 1 + xBgBowl + (iimgSetBarWidth * getRandomInt(4));
                //asalna yMatterBlock[i] = yBgBowl - (i * 4 * iJarak);
                yMatterBlock[i] = yBgBowl;
            }
            dasar = MMIDlet.HEIGHT - (imgSetResultBar.getHeight() + 18);
            //System.out.println("Dasar : " + dasar);
            xAnswer = xSetResultBar + 165;
            yAnswer = ySetResultBar + 5;

            xAlarmMo = 25;
            yAlarmMo = 25;
            xMoroEnemy = 10;
            yMoroEnemy = 20;
            xMoroMine = imgSet.getWidth();
            yMoroMine = ySetResultBar + imgMoro.getHeight();
            //dasar = 320-28;
            //System.out.println("cikan! : " + imgSetResultBar.getHeight() + 18);
        }

        spAlarmMo.setPosition(xAlarmMo, yAlarmMo);
        spMoroEnemy.setPosition(xMoroEnemy, yMoroEnemy);
        spMoroMine.setPosition(xMoroMine, yMoroMine);
        //spBgBowl.setPosition(xBgBowl, yBgBowl);


        try {
            sound01 = Manager.createPlayer(getClass().getResourceAsStream("/game.mid"), "audio/midi");
            sound01.realize();
            sound01.prefetch();
            sound01.addPlayerListener(this);
            sound01.setLoopCount(-1);
            if (midlet.SOUND == 1) {
                sound01.start();
            } else {
            }

        } catch (Exception e) {
        }
    }

    //start thread
    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    //game loop
    public void run() {
        Graphics g = getGraphics();
        while (running) {
            //checkForCollision();
            drawDisplay(g);
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }
    }

    public void stop() {
        running = false;
    }

    private void showGrid(Graphics g) {
        g.setColor(0x0000ff);
        int x1 = 0;
        int y1 = 0;

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 10; j++) {
                //grid[i][j] = 10;

                //x1 = xBgBowl + (i * imgSetBarRed.getWidth());
                //y1 = dasar - (j * (imgSetBarRed.getHeight()));

                g.setColor(0xffffff);
                g.drawString(String.valueOf(grid[i][j]),
                        x1,
                        y1,
                        Graphics.TOP | Graphics.LEFT);
                //g.drawLine(0, y1,
                //        x1 + (i * imgSetBarRed.getWidth()),
                //        y1);
            }
        }
    }

    protected void drawDisplay(Graphics g) {
        //g.setColor(0xffffff);
        //g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(0x000000);
        //g.drawString("for test only : " + getWidth() + "x" + getHeight(), 0, 10, Graphics.TOP | Graphics.LEFT);
        g.drawImage(imgBarBowl, xBarBowl, yBarBowl, Graphics.TOP | Graphics.LEFT);
        g.drawImage(imgBgBowl, xBgBowl, yBgBowl, Graphics.TOP | Graphics.LEFT);
        g.drawImage(imgSetResultBar, xSetResultBar, ySetResultBar, Graphics.TOP | Graphics.LEFT);
        //g.drawImage(imgPortalLimit, xBarBowl, yBarBowl, Graphics.TOP | Graphics.LEFT);

        //animated sprite, show next frame in sequence        
        downMatterBlock();
        showGrid(g);
        showMatterBlock(g);

        spAlarmMo.nextFrame();
        showMoroEnemy();
        //paint all layers
        lmgr.paint(g, 0, 0);

        showMatterString(g);
        showGrid(g);

        flushGraphics();
    }

    private void showMatterBlock(Graphics g) {
        for (int i = 0; i < JUM; i++) {
            //matterBlock[posisi[i]].setPosition(xMatterBlock[i], yMatterBlock[i]);
            matterBlock[i].setPosition(xMatterBlock[i], yMatterBlock[i]);
            spAncurBlock[i].setPosition(xMatterBlock[i], yMatterBlock[i]);
            //spAncurBlock[i].nextFrame();
        }
    }

    private void hiddenMatterBlock(int id) {
        matterBlock[id].setVisible(false);
    }

    private void showMatterString(Graphics g) {
        g.setFont(MMIDlet.SMALL_BOLD_FONT);
        //g.fillRect(matterBlock.getX() + 2, matterBlock.getY() - 7, 48, 10);
        //System.out.println(matterBlock.getX() + 2);
        g.setColor(0x000000);
        for (int i = 0; i < JUM; i++) {
            //nanti //g.drawString(matterBlock[i].getMatter(), matterBlock[i].getX() + 5, matterBlock[i].getY(),
            //        Graphics.TOP | Graphics.LEFT);


            if (matterBlock[i].isBucat() == false
                    && matterBlock[i].isVisible() == true) {
                g.drawString(matterBlock[i].getQuestion().matter,
                        matterBlock[i].getX() + 5, matterBlock[i].getY(),
                        Graphics.TOP | Graphics.LEFT);
            }
        }

        g.drawString(sAnswer,
                xAnswer, yAnswer,
                Graphics.TOP | Graphics.LEFT);

        //g.drawString("cikaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaan", 0,
        //        10, Graphics.TOP | Graphics.LEFT);

        //g.fillRect(40, matterBlock.getY() + 5, 10, 13);
    }

    private void turunkan() {
    }

    private boolean checkCollides(int id) {
        boolean kena = false;
        for (int i = 0; i < JUM; i++) {
            if (matterBlock[id].collidesWith(matterBlock[i], true)) {
                kena = true;
            } else {
                kena = false;
            }
        }
        return kena;
    }

    private void downMatterBlock() {
        if (matterBlock[0].isStacked() == false) {
            yMatterBlock[0] = yMatterBlock[0] + speedMatterBlock;
        }
        for (int i = 0; i < JUM; i++) {
            if (matterBlock[i].isStacked() == false && yMatterBlock[i] < dasar) {
                //System.out.println(i + " - " + matterBlock[i].isStacked() +
                //        matterBlock[i].getQuestion().answer);

                
                //if (isKenaDeh == true) {
                    yMatterBlock[i] = yMatterBlock[i] + speedMatterBlock;
                    //isKenaDeh = false;
               // }
                
                /*
                if (i != 0) {
                if (yMatterBlock[i - 1] > (yBgBowl + 50)) {
                yMatterBlock[i] = yMatterBlock[i] + speedMatterBlock;
                }
                } else {
                yMatterBlock[i] = yMatterBlock[i] + speedMatterBlock;
                }
                 * 
                 */               

                /*
                if(isKenaDeh==true){

                isKenaDeh = false;
                }
                 * 
                 */
                /*
                if(yMatterBlock[i] < 40){
                if(checkCollides(i)==true){
                reRandom(i);
                }
                }
                 * 
                 */

                /*
                if ((yMatterBlock[i]) > yBgBowl) {
                matterBlock[i].setVisible(true);
                }
                 * 
                 */

                for (int j = 0; j < JUM; j++) {
                    //System.out.println("kesini ga ");
                    if (yMatterBlock[i] > (yBgBowl + 50)) {
                        /*
                        System.out.println("siiiiiiiiih ");
                        if (((yMatterBlock[i] + matterBlock[i].getHeight()) == yMatterBlock[j])
                                && (xMatterBlock[i] == xMatterBlock[j])) {
                            matterBlock[i].setStacked(true);
                            isKenaDeh = true;
                            
                            System.out.println(i + " : " + i + " true! " +
                                    matterBlock[i].getX() + ", " + matterBlock[i].getY());
                        }
                         * 
                         */
                    }
                }
            } else {//if(matterBlock[i].isStacked() == false && yMatterBlock[i] >= dasar) {
                matterBlock[i].setPosition(xMatterBlock[i], dasar);
                matterBlock[i].setStacked(true);
                //isKenaDeh = true;
                System.out.println(i + " : " + i + " dasar! " + matterBlock[i].getX() + ", " + matterBlock[i].getY());
            }
            if (matterBlock[i].isStacked() == true && matterBlock[i].isBucat() == true) {
                matterBlock[i].setVisible(false);
                spAncurBlock[i].setVisible(true);
                spAncurBlock[i].nextFrame();
                if (spAncurBlock[i].getFrame() == 8) {
                    matterBlock[i].setBucat(false);
                    matterBlock[i].setStacked(false);
                    spAncurBlock[i].setVisible(false);
                    reRandom(i);
                }
            }
        }
    }

    private void showMoroEnemy() {
        spMoroEnemy.nextFrame();
        spMoroMine.nextFrame();
    }

    private void showMoroMine() {
    }

    private void reRandom(int id) {
        //int testing = 1; //4
        matterBlock[id].setVisible(true);

        xMatterBlock[id] = 1 + xBgBowl + (iimgSetBarWidth * getRandomInt(4));
        //asalna true //yMatterBlock[id] = yBgBowl - ((getRandomInt(4) + 2) * testing * iJarak);//yBgBowl;// - 40;//
        //asalna yMatterBlock[id] = yBgBowl - ((getRandomInt(4) + 2) * testing * iJarak);//testing!
        //asalna yMatterBlock[id] = yBgBowl - (id * 4 * iJarak);

        yMatterBlock[id] = yBgBowl;

        matterBlock[id].setPosition(xMatterBlock[id], yMatterBlock[id]);

        lastIDforRandom = id;

        reRandomQuestion(id);

        /*
        int warna = getRandomInt(3);
        if (warna == 0) {
        //matterBlock[id].setImage(imgSetBarRed, xMatterBlock[id], yMatterBlock[id]);
        //matterBlock[id] = new MatterBlock(imgSetBarRed,
        //        xMatterBlock[id], yMatterBlock[id]);
        //spAncurBlock[id] = new Sprite(imgAncurBlockRed,
        //        60, 28);
        } else if (warna == 1) {
        //matterBlock[id].setImage(imgSetBarYellow, xMatterBlock[id], yMatterBlock[id]);
        //matterBlock[id] = new MatterBlock(imgSetBarYellow,
        //        xMatterBlock[id], yMatterBlock[id]);
        //spAncurBlock[id] = new Sprite(imgAncurBlockYellow,
        //        60, 28);
        } else if (warna == 2) {
        //matterBlock[id] = new MatterBlock(imgSetBarBlue,
        //        xMatterBlock[id], yMatterBlock[id]);
        //matterBlock[id].setImage(imgSetBarBlue, xMatterBlock[id], yMatterBlock[id]);
        //spAncurBlock[id] = new Sprite(imgAncurBlockBlue,
        //        60, 28);
        }
         * 
         */


        //}


        //still error outof memmory
        //if (matterBlock[id].collidesWith(matterBlock[lastIDforRandom], true)==true) {
        /*
        if ((matterBlock[id].getX() == matterBlock[lastIDforRandom].getX()) &&
        (matterBlock[id].getY() == matterBlock[lastIDforRandom].getY())){
        //ulangi = true;
        //reRandom(id);
        matterBlock[id].setPosition(xMatterBlock[id], yMatterBlock[id]-100);
        }
         * 
         */



    }

    private void mathAttack(String s) {
        System.out.println(xBgBowl + ", " + dasar);
        String answerTemp = "";
        boolean ketemu = false;

        for (int i = 0; i < JUM; i++) {
            if (matterBlock[i].getY() > yBgBowl && matterBlock[i].getY() <= dasar + 1) {
                if (matterBlock[i].getQuestion().answer.equals(sAnswer)) {
                    matterBlock[i].setStacked(true);
                    matterBlock[i].setBucat(true);
                    //sAnswer = "";
                    //ketemu = true;
                    System.out.println("q[" + i + "]" + matterBlock[i].getQuestion().answer + " - " + sAnswer);
                    //break;
                    //answerTemp = matterBlock[i].getQuestion().answer;
                    //sudah = true;
                    //
                }

                /*else if (answerTemp.equals(matterBlock[i].getQuestion().answer)
                && sudah == true) {
                matterBlock[i].setStacked(true);
                matterBlock[i].setBucat(true);
                sudah = false;
                //System.out.println("nu ke-2 q[" + i + "]" + matterBlock[i].getQuestion().answer + " - " + sAnswer);
                }
                 * 
                 */
            }
        }
        //search:
        System.out.println("UDAH!");
    }

    protected void keyPressed(int keyCode) {
        String statusKey = String.valueOf(keyCode);
        //if (MathackMIDlet.LANDSCAPE == 1) { //landscape
        if (keyCode == -5) {
            mathAttack(sAnswer);
            //System.out.println("dah!");
            sAnswer = "";
        } else if (keyCode == -2) { //down
        } else if (keyCode == -4) { //right
        } else if (keyCode == -1) { //up
        } else if (keyCode == -3) { //left
        } else if (keyCode == -7) {
            midlet.exit();
        } else if (keyCode == -6) {
        } //yo
        //asha 300
        else if (keyCode == 48) { //1
            sAnswer = sAnswer + 0;
        } else if (keyCode == 49) { //1
            sAnswer = sAnswer + 1;
        } else if (keyCode == 50) { //LA-UP 2
            sAnswer = sAnswer + 2;
        } else if (keyCode == 51) { //
            sAnswer = sAnswer + 3;
        } else if (keyCode == 52) { //LA-LEFT 4
            sAnswer = sAnswer + 4;
        } else if (keyCode == 53) { //
            sAnswer = sAnswer + 5;
        } else if (keyCode == 54) { //LA-RIGHT 6
            sAnswer = sAnswer + 6;
        } else if (keyCode == 55) { //
            sAnswer = sAnswer + 7;
        } else if (keyCode == 56) { //LA-DOWN 8
            sAnswer = sAnswer + 8;
        } else if (keyCode == 57) { //
            sAnswer = sAnswer + 9;
        } //yo
        //QWERY 
        else if (keyCode == 106) { //1
            mathAttack(sAnswer);
            //System.out.println("dah!");
            sAnswer = "";
        } else if (keyCode == 109) { //1
            sAnswer = sAnswer + 0;
        } else if (keyCode == 114) { //1
            sAnswer = sAnswer + 1;
        } else if (keyCode == 116) { //
            sAnswer = sAnswer + 2;
        } else if (keyCode == 121) { //
            sAnswer = sAnswer + 3;
        } else if (keyCode == 102) { //
            sAnswer = sAnswer + 4;
        } else if (keyCode == 103) { //
            sAnswer = sAnswer + 5;
        } else if (keyCode == 104) { //
            sAnswer = sAnswer + 6;
        } else if (keyCode == 118) { //
            sAnswer = sAnswer + 7;
        } else if (keyCode == 98) { //
            sAnswer = sAnswer + 8;
        } else if (keyCode == 110) { //
            sAnswer = sAnswer + 9;
        }

        repaint();
    }

    public void playerUpdate(Player arg0, String eventType, Object arg2) {
        if (eventType.equals(STARTED)) {
            delay = System.currentTimeMillis() - delay;
            repaint();
        } else if (eventType.equals(END_OF_MEDIA)) {
            delay = System.currentTimeMillis();
        }
    }

    private int getRandomInt(int upper) {
        int retVal = myRandom.nextInt() % upper;
        if (retVal < 0) {
            retVal += upper;
        }
        return (retVal);
    }

    private int[] getArrayNoRepeatRandom(int jum, int range) {
        //Delcare new Random object
        Random rand = new Random();

        //Intialize variables
        //int JUM = 40;
        //int RANGE = 40;
        int number, nRCount = 0;
        int[] array = new int[jum];
        int[] returnArray = new int[jum];
        boolean fill;


        do {
            fill = true;
            //Get new random number
            number = rand.nextInt(range) + 1;
            //number = rand.nextInt(MAX);

            //If the number exists in the array already, don't add it again
            for (int i = 0; i < array.length; i++) {
                if (array[i] == number) {
                    fill = false;
                }
            }
            //If the number didn't already exist, put it in the array and move
            //To the next position.
            if (fill == true) {
                array[nRCount] = number;
                nRCount++;
            }
        } while (nRCount < jum);
        for (int i = 0; i < array.length; i++) {
            //System.out.print(array[i] - 1 + " ");
            returnArray[i] = array[i] - 1;
        }

        return returnArray;
    }

    private void randomQuestion() {
        for (int i = 0; i < JUM; i++) {
            Question q = new Question();
            int rQ = getRandomInt(2);
            if (rQ == 0) {
                q.setMatter(getRandomInt(MAX_RANGE), "+", getRandomInt(MAX_RANGE));
                //System.out.println(q.matter);
                //System.out.println("m : " + matterBlock[i].getQuestion().matter);
                matterBlock[i].setQuestion(q);
            } else {
                q.setMatter(getRandomInt(MAX_RANGE), "+", getRandomInt(MAX_RANGE));
                matterBlock[i].setQuestion(q);
            }
        }
    }

    private void reRandomQuestion(int id) {
        Question q = new Question();
        int rQ = getRandomInt(2);
        if (rQ == 0) {
            q.setMatter(getRandomInt(MAX_RANGE), "+", getRandomInt(MAX_RANGE));
            //System.out.println(q.matter);
            //System.out.println("m : " + matterBlock[i].getQuestion().matter);
            matterBlock[id].setQuestion(q);
        } else {
            q.setMatter(getRandomInt(MAX_RANGE), "+", getRandomInt(MAX_RANGE));
            matterBlock[id].setQuestion(q);
        }
    }
}
